package com.zhiyou100.javaweb.myservlet.day004;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @packageName: javase_26
 * @className: ${NAME}
 * @Description: TODO Response 功能
 * @author: yang
 * @date: 2020/5/25
 */
@WebServlet(name = "Day004ServletResponse",urlPatterns = "/day004/s1")
public class Day004ServletResponse extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*
         * 1、设置响应体
         * void setContentType(""); 设置响应内容的格式和编码集，等价于设置响应头contentType
         * void setCharacterEncoding("UTF-8"); 设置输出流的编码集
         * PrintWriter getWriter();  获取字符输出流
         * ServletOutputStream getOutputStream(StandardCharsets.UTF_8); 获取字节输出流
         *
         * 注意：
         * getWriter和getOutputStream不能同时使用
         */
        response.setContentType("text/html;charset=UTF-8");
        // 形影输出流使用utf-8
        response.setCharacterEncoding("UTF-8");
        // 只设置输出流的编码集
//        PrintWriter writer = response.getWriter();
        // 获取字符输出流
//        writer.write("<h1>字符输出流</h1>");
//        writer.close();
//        ServletOutputStream outputStream = response.getOutputStream();
        // 获取字节输出流
//        outputStream.write("<h1>字符输出流</h1>".getBytes(StandardCharsets.UTF_8));

        /**
         * 2. 设置响应头
         * void setHeader(String name,String value) 设置响应头
         * void setStatus(); 设置状态码
         */

        response.setHeader("a","hearAa");


        /**
         * 3 设置重定向
         * 绘图
         * void sendRedirect (地址) 重定向
         * sendRedirect 等价于设置响应状态码 302 + 设置响应头 location
         */
        response.sendRedirect("http://localhost:1024/javase_26/day004/s2");

        /**
         * 4. 其他功能(方法)
         * void addCookie(Cookie) 添加 cookie
         * void sendError(statusInt,message) 设置错误状态码 + 错误信息
         */


    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
